import pygmail
import operator
import time

class responseProcessing(object):
	"""Default directory is current."""

	def __init__(self,numOfQuestions):
		"""Initializes."""
		self.responses={}
		self.numOfQuestions=numOfQuestions

	def user(self,user):
                """Sets user value."""
		self.user=user

	def pass_(self,passw):
                """Sets password value."""
		self.password=passw

	def connect(self):
                """Connects to gmail."""
		self.M = pygmail.pygmail()
		self.M.login(self.user,self.password)
				
	def disconnect(self):
                """Disconnects from gmail."""
		self.M.logout()

        def clearInbox(self):
                """Deletes all current messages in inbox."""
                msgList=self.M.get_mails_unread()
		for msgId in msgList:
			self.M.set_mail_deleted_from_id(msgId)
		self.M.mark_flags()

	def collect(self):
                """Collects messages from Inbox and Deletes.

                Puts messages in self.messages."""
		self.messages=[]
		msgList=self.M.get_mails_unread()
		for msgId in msgList:
			self.messages.append(self.M.get_mail_from_id(msgId)[0][1])
		self.M.mark_flags()
		for msgId in msgList:
			self.M.set_mail_deleted_from_id(msgId)
		self.M.mark_flags()

	def extractMessages(self):
                """Extracts sms from email messages.

                Also makes everything lowercase."""
		self.smsFinal=[]
		for msg in self.messages:
			self.smsFinal.append(msg.split('\r\n')[0].lower())

	def extractResponses(self):
                """Records people and answers from SMS.

                Responses stored in self.responses."""
                self.peopleNew=[]
		for sms in self.smsFinal:
			self.people=[]
			self.smsSplitSpaces=sms.split(' ')
			self.smsSplit=[]
			for entry in self.smsSplitSpaces:
                                if entry != '':
                                        self.smsSplit.append(entry)
			for part in self.smsSplit:
				if part[0] == 'w':
					self.people.append(part.split('w')[1])
					self.peopleNew.append(part.split('w')[1])
			for person in self.people:
				self.smsSplit.pop(0)
			if len(self.smsSplit) == self.numOfQuestions:
				for person in self.people:
                                        self.responses[person]=self.smsSplit

        def collectionLoop(self):
                """Executes all collection and extraction with a pause."""
                self.collect()
                self.extractMessages()
                self.extractResponses()

	def responseSummary(self):
                """Summarizes responses in array of dicts."""
		self.summary=[]
		for question in range(self.numOfQuestions):
                        self.summary.append({})
                        for person in self.responses:
                                if self.responses[person][question] in self.summary[question]:
                                        self.summary[question][self.responses[person][question]]=self.summary[question][self.responses[person][question]]+1
                                else:
                                        self.summary[question][self.responses[person][question]]=1

	def sortResponses(self):
                """Changes response summaries to sorted lists."""
                self.responseSummary()
		self.sortedResponses=[]
		for question in self.summary:
                        x=sorted(question.iteritems(), key=operator.itemgetter(1))
                        x.reverse()
			self.sortedResponses.append(x)
